package com.dogkr.server.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dogkr.server.entity.Host;
import com.dogkr.server.entity.HostApply;
import com.dogkr.server.entity.PetServer;
import com.dogkr.server.entity.PetServerValuation;
import com.dogkr.server.mapper.HostApplyMapper;
import com.dogkr.server.mapper.HostMapper;
import com.dogkr.server.mapper.SysPetServerMapper;
import com.sean.common.page.Pagination;
import com.sean.common.page.ResultPagination;
import com.sean.common.service.BaseService;
import com.sean.common.utils.StringUtils;


@Service
public class ManagerService extends BaseService {
	
	@Autowired
	private HostMapper hostMapper;
	@Autowired
	private HostApplyMapper hostApplyMapper;
	
	@Autowired
	private SysPetServerMapper sysPetServerMapper;
	
	@Autowired
	private PetServerService petServerService;

	public ResultPagination<HostApply> selectApply(Pagination pagination) {
		return this.selectListForPage(HostApplyMapper.class, "selectWithSearch", pagination, HostApply.class);
	}

	public void verifyApply(Long id) {
		HostApply apply = hostApplyMapper.selectByPrimaryKey(id);
		Host host = new Host();
		host.create(apply);
		hostMapper.insert(host);
		apply.setHostId(host.getId());
		apply.setStatus(HostApply.STATUS_NORMAL);
		hostApplyMapper.updateByPrimaryKeySelective(apply);
		//添加基础服务价格
		PetServer hostServer = this.createBaseServer(apply);
		List<PetServer> saveServerList = new ArrayList<PetServer>();
		saveServerList.add(hostServer);
		petServerService.saveHostServerWithAll(saveServerList, host.getId());
	}
	
	private PetServer createBaseServer(HostApply apply) {
		PetServer sysServer = sysPetServerMapper.selectByCode("base");
		PetServerValuation valuation = sysServer.getPetServerValuationList().get(0);
		valuation.setSysId(valuation.getId());
		valuation.setValueVal(apply.getServerVal());
		return sysServer;
	}
}
